package com.honglang.rss.impl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;



public class RSSEventWriter extends XmlEventWriter{

	public RSSEventWriter(String file,String encoding) throws FileNotFoundException,XMLStreamException {
		super(file,encoding);
		// TODO Auto-generated constructor stub
	}
	
	protected void startRss() throws XMLStreamException{
		this.getXmlEventWriter().add(this.getEnd());
		Attribute attribute = this.getXmlEventFactory().createAttribute("version", "2.0");
		List<Attribute>  attributeList = new ArrayList<Attribute>();
		attributeList.add(attribute);
		StartElement rssStartElement = this.getXmlEventFactory().createStartElement("", "", "rss",attributeList.iterator() , null);
		this.getXmlEventWriter().add(rssStartElement);
		this.getXmlEventWriter().add(this.getEnd());
	}
	
	
	protected void endRss() throws XMLStreamException{
		this.addSimpleEndTag("rss");
	}
	
	
	protected void addChannel(Channel channel) throws XMLStreamException{
		this.getXmlEventWriter().add(this.getTab());
		this.addSimpleStartTag("channel");
		for(Method method : channel.getClass().getMethods()){
			if(method.getReturnType().equals(String.class) && method.getName().substring(0, 3).equals("get")){
				try {
					String value = (String)method.invoke(channel, new Object[]{});
					
					if(value != null && !value.isEmpty()){
						
						this.createNode(method.getName().substring(3,4).toLowerCase() + method.getName().substring(4),value,2 );
					}
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		for(Item item :channel.getItemList()){
			this.getXmlEventWriter().add(this.getTab());
			this.getXmlEventWriter().add(this.getTab());
			StartElement itemStartElement = this.getXmlEventFactory().createStartElement("", "", "item");
			this.getXmlEventWriter().add(itemStartElement);
			this.getXmlEventWriter().add(this.getEnd());
			this.addItem(item);
			EndElement itemEndElement = this.getXmlEventFactory().createEndElement("", "", "item");
			this.getXmlEventWriter().add(this.getTab());
			this.getXmlEventWriter().add(this.getTab());
			this.getXmlEventWriter().add(itemEndElement);
			this.getXmlEventWriter().add(this.getEnd());
		}
		this.getXmlEventWriter().add(this.getTab());
		this.addSimpleEndTag("channel");
	}
	
	
	
	
	protected void addItem(Item item) throws XMLStreamException{
		for(Method method : item.getClass().getMethods()){
			if(method.getReturnType().equals(String.class) && method.getName().substring(0, 3).equals("get")){
				try {
					String value = (String)method.invoke(item, new Object[]{});
					if(value != null && !value.isEmpty()){
						this.createNode(method.getName().substring(3,4).toLowerCase() + method.getName().substring(4),value,3);
					}
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	protected void addSimpleStartTag(String localName) throws XMLStreamException{
		StartElement startElement = this.getXmlEventFactory().createStartElement("",
				"", localName);
		this.getXmlEventWriter().add(startElement);
		this.getXmlEventWriter().add(this.getEnd());
	}
	
	protected void addSimpleEndTag(String localName) throws XMLStreamException{
		EndElement endElement = this.getXmlEventFactory().createEndElement("","", localName);
		this.getXmlEventWriter().add(endElement);
		this.getXmlEventWriter().add(this.getEnd());
	}
	
	
	
	public void  createRss(List<Channel> channelList) throws XMLStreamException{
		this.startDocument(this.getEncoding());
		this.startRss();
		for(Channel channel : channelList){
			this.addChannel(channel);
		}
		this.endRss();
		this.endDocument();
		
	}
	
	
	
	
	public void createRss(Channel channel) throws XMLStreamException, IOException{
		this.startDocument(this.getEncoding());
		this.startRss();
		this.addChannel(channel);
		this.endRss();
		this.endDocument();
	}
	
	
	
	
	
	public static  void main(String[] args){
		Item item = new Item();
		item.setTitle("123");
		item.setDescription("Sdfsa$&<\nfsdfasd");
		
		Item item1 = new Item();
		item1.setTitle("123");
		item1.setDescription("Sdfs 的。dfasd");
		Channel channel = new Channel("aaa","cccc","ddd");
		channel.addItem(item);
		channel.addItem(item1);
		
		try {
			RSSEventWriter rssWriter = new RSSEventWriter("E:\\a.xml","UTF-8");
			rssWriter.createRss(channel);
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	
	
	
	
}
